﻿using Bmz.IM.Communication;
using Bmz.IM.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Bmz.IM.Server
{
    public class IMServer
    {
        FriendInfo friendInfo = new FriendInfo();
        Dictionary<string, UserVO> allUserDict = new Dictionary<string, UserVO>();

        public void Initialize(IPEndPoint ipEndPoint)
        {
            ServerEngineContext.Current.Initialize(ipEndPoint);
            ServerEngineContext.Current.EventDict.Add(StructMessageType.InitMainForm, InitMainFormEvent);
            ServerEngineContext.Current.EventDict.Add(StructMessageType.Login, LoginEvent);
            ServerEngineContext.Current.EventDict.Add(StructMessageType.Logout, LogoutEvent);
        }

        private void LoginEvent(MessageVO messageVO)
        {
            UserInfo userInfo = new UserInfo();
            string[] strs = messageVO.Content.Split('|');
            string userId = strs[0].Split(':')[1];
            string pwd = strs[1].Split(':')[1];
            string state = strs[2].Split(':')[1];

            if (userInfo.Authentication(userId, pwd))
            {
                UserVO userVO = userInfo.GetUser(userId);
                userVO.Ip = messageVO.FromIp;
                userVO.Port = messageVO.FromPort;
                userVO.CurrentState = state;
                messageVO.Content = JsonConvert.SerializeObject(userVO);

                if (!allUserDict.ContainsKey(userVO.UserId))
                {
                    allUserDict.Add(userVO.UserId, userVO);
                }
                else
                {
                    allUserDict.Remove(userVO.UserId);
                    allUserDict.Add(userVO.UserId, userVO);
                }
                List<FriendVO> friendList = friendInfo.GetFriends(userVO.UserId);
                foreach (FriendVO item in friendList)
                {
                    if (allUserDict.ContainsKey(item.UserId))
                    {
                        UserVO firend = allUserDict[item.UserId];
                        MessageVO friendMessage = new MessageVO()
                        {
                            Type = StructMessageType.FriendLogin,
                            Content = JsonConvert.SerializeObject(userVO),
                            FromUserId = "server",
                            FromIp = "",
                            FromPort = 0,
                            ToUserId = firend.UserId,
                            ToIp = firend.Ip,
                            ToPort = firend.Port
                        };
                        ServerEngineContext.Current.SendMessageUdp(friendMessage);
                    }
                }
            }
            else
            {
                messageVO.Content = "LoginFail";
            }
            messageVO.ReversalFromAndToInfo();
            ClientEngineContext.Current.SendMessageUdp(messageVO);
        }

        private void InitMainFormEvent(MessageVO messageVO)
        {
            List<FriendVO> friendList = friendInfo.GetFriends(messageVO.FromUserId);
            foreach (FriendVO item in friendList)
            {
                if (allUserDict.ContainsKey(item.UserId))
                {
                    UserVO user = allUserDict[item.UserId];
                    item.Ip = user.Ip;
                    item.Port = user.Port;
                    item.CurrentState = user.CurrentState;
                }
            }
            messageVO.Content = JsonConvert.SerializeObject(friendList);
            messageVO.ReversalFromAndToInfo();
            ClientEngineContext.Current.SendMessageUdp(messageVO);
        }

        private void LogoutEvent(MessageVO messageVO)
        {
            allUserDict.Remove(messageVO.FromUserId);
            List<FriendVO> friendList1 = friendInfo.GetFriends(messageVO.FromUserId);
            foreach (FriendVO item in friendList1)
            {
                if (allUserDict.ContainsKey(item.UserId))
                {
                    UserVO firend = allUserDict[item.UserId];
                    MessageVO friendMessage = new MessageVO()
                    {
                        Type = StructMessageType.FriendLogout,
                        Content = messageVO.Content,
                        FromUserId = "server",
                        FromIp = "",
                        FromPort = 0,
                        ToUserId = firend.UserId,
                        ToIp = firend.Ip,
                        ToPort = firend.Port
                    };
                    ServerEngineContext.Current.SendMessageUdp(friendMessage);
                }
            }
        }
    }
}
